#include <stdio.h>
#include <getopt.h>
#include "crypt/crypt.h"
#include <string>
#include <windows.h>
#include <time.h>
#include <util.h>
#include <iostream>
#include "crypt/encdec.h"
struct option options[] = {
    {"help", no_argument, 0, 'h'},
    {"days", required_argument, 0, 'd'},
    {"file-path", required_argument, 0, 'p'},
    {"privateKey", required_argument, 0, 'k'},
    {0, 0, 0, 0}};

const char *shortopt = "p:hd:";

int main(int argc, char **argv)
{

  int optindex = 0;
  int tag;

  int days = 0;

  char filepath_tmp[256] = {0};
  GetModuleFileNameA(NULL, filepath_tmp, 256);
  char *pindex = strrchr(filepath_tmp, '\\');
  *pindex = '\0';

  std::string token_path(filepath_tmp);

  while ((tag = getopt_long(argc, argv, shortopt, options, &optindex)) != -1)
  {

    printf("====%d,%d\n", optind, optindex);

    switch (tag)
    {
    case 'h':
      printf("usage: -d expire days -p filepath \n");
      return 0;
    case 'd':
      printf("days=%s\n", optarg);
      days = atoi(optarg);
      break;
    case 'p':
      printf("days=%s\n", optarg);

      token_path = optarg;
      break;
    case '?':
      printf("unvaliable param:%c \n", optopt);
      break;
    default:
      printf("unknown option\n");
      break;
    }
  }

  if (token_path.length() > 0 && days > 0)
  {

    create_token(token_path.c_str(), days);
  }

  TokenManager token(token_path.c_str());

  // const char * srcdata="1";

  // unsigned char * result;

  // int siglen=rsa_sha256_sign((const unsigned char *)srcdata,strlen(srcdata),&result);

  // int ret=rsa_sha256_verify((const unsigned char *)srcdata,strlen(srcdata),result,siglen);

  // if (ret==1)
  // {
  //   printf("=======================verify success\n");
  // }

  // printf("token expire:%d\n",token.token_expire());

  return 0;
}